/**
 * @param target 当前需要校验的环境
 */
type EnvironType = 'applets' | 'weChat' | 'app' | 'pc'
const Environ = (target: EnvironType | '') => {
  switch (target) {
    case 'applets':
      const applets =
        navigator.userAgent.match(/micromessenger/i) != null &&
        navigator.userAgent.match(/miniprogram/i) != null
      return applets ? 'applets' : ''
      break
    case 'weChat':
      const weChat =
        navigator.userAgent.match(/micromessenger/i) != null &&
        navigator.userAgent.match(/miniprogram/i) == null
      return weChat ? 'weChat' : ''
      break
    case 'app':
      const isIos = !!(
        (window.navigator && window.navigator.userAgent) ||
        ''
      ).match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
      const isAndroid =
        ((window.navigator && window.navigator.userAgent) || '').indexOf(
          'Android'
        ) > -1 ||
        ((window.navigator && window.navigator.userAgent) || '').indexOf(
          'Adr'
        ) > -1
      if (isIos) return 'ios'
      if (isAndroid) return 'android'
      if (!isIos && !isAndroid) return 'browser'
      break
    case 'pc':
      const ua = navigator.userAgent.toLowerCase() //获取用户端信息
      const info = {
        ie: /msie/.test(ua) && !/opera/.test(ua), //匹配IE浏览器
        op: /opera/.test(ua), //匹配Opera浏览器
        sa: /version.*safari/.test(ua), //匹配Safari浏览器
        ch: /chrome/.test(ua), //匹配Chrome浏览器
        ff: /gecko/.test(ua) && !/webkit/.test(ua), //匹配Firefox浏览器
      }
      if (info.ie) return 'ie'
      if (info.op) return 'opera'
      if (info.sa) return 'safari'
      if (info.ch) return 'chrome'
      if (info.ff) return 'firefox'
      return ''
      break
  }
}

export default Environ
